package com.ys.controller.web.hr;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.entity.business.ThrOperateCompanyBrand;
import com.ys.entity.business.ThrOperateCompanyEuropeTrade;
import com.ys.entity.business.ThrOperateCompanyVps;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.service.sys.CommonApiService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营公司管理表
 */
@Controller
@RequestMapping("/web/hr/thrOperateCompanyApply")
public class WebThrOperateCompanyApplyController {
	private static final Logger logger = LoggerFactory.getLogger(WebThrOperateCompanyApplyController.class);

	@Autowired
	private CommonApiService<ThrOperateCompanyApply> commonApiService;


	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,Long id,String editType){
		try {
			Map<String,Object> entity = new ThrOperateCompanyApply()
					.addLeftJoin("TS_UserChange B on B.Id = A.OperatorChangeId")
					.addField("B.UserId operatorUserId")//运营人工号
					.addField("B.UserName operatorUserName")//运营人
					.addField("B.RoleName roleName")//运营人岗位
					.addField("B.DeptName deptName")//运营人部门

					.addLeftJoin("TS_OperateOrg O on O.Id = A.OperateOrgId")
					//.addLeftJoin("TS_OperateOrg PO on PO.Id = O.Pid AND 1=1")
					.addLeftJoin("THR_OperateCompanyApply PO ON O.HeadVgsbm = PO.Vgsbm and 1=1")
					.addField("O.Name orgName")//机构名称
					//.addField("O.Pid orgbm")//上级公司编码
					//.addField("PO.Name orgJc")//上级公司简称
					.addField("PO.Vgsbm AS orgbm")//上级公司编码
					.addField("PO.Vgsjc AS orgJc")//上级公司简称

					.setId(id)
					.queryFirstMap();

			Map<String, Object> extraParam = new HashMap<String, Object>();
			//关联数据
			if(entity.get("operatorUserId")!=null)extraParam.put("operatorUserId", entity.get("operatorUserId"));
			if(entity.get("operatorUserName")!=null)extraParam.put("operatorUserName", entity.get("operatorUserName"));
			if(entity.get("roleName")!=null)extraParam.put("roleName", entity.get("roleName"));
			if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
			if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));
			if(entity.get("orgbm")!=null)extraParam.put("orgbm", entity.get("orgbm"));
			if(entity.get("orgJc")!=null)extraParam.put("orgJc", entity.get("orgJc"));

			//获取运营公司表VPS信息
			List<Map<String, Object>> vpsList = new ThrOperateCompanyVps()
					.addField("'"+entity.get("vgsjc") + "' AS vgsjc")
					.setVgsbm(entity.get("vgsbm").toString())
					.queryMap();
			//获取运营公司表品牌信息
			List<Map<String, Object>> brandList = new ThrOperateCompanyBrand()
					.addField("'"+entity.get("vgsjc") + "' AS vgsjc")
					.addField("R.Name AS countryName")
					.setVgsbm(entity.get("vgsbm").toString())
					.addLeftJoin("TB_Country R ON A.CountryCode = R.Code")
					.queryMap();

			//获取运营公司表欧洲贸易合规
			List<Map<String, Object>> europeTradeList = new ThrOperateCompanyEuropeTrade()
					.addField("'"+entity.get("vgsjc") + "' AS vgsjc")
					.addField("R.Name AS countryName")
					.addField("R2.Name AS eoriCountryName")
					.setVgsbm(entity.get("vgsbm").toString())
					.addLeftJoin("TB_Country R ON A.CountryCode = R.Code")
					.addLeftJoin("TB_Country R2 ON A.EoriCountryCode = R2.Code")
					.queryMap();

			DetailList detailList = new DetailList(
					Long.parseLong(entity.get("id").toString()),
					entity.get("vgsbm").toString(),
					entity.get("vgsjc").toString(),
					vpsList,
					brandList,
					europeTradeList);
			model.addAttribute("detail", detailList);

			Long menuId = MenuId.THR_OperateCompanyApply_By_Archives;
			if(editType.equals("operations")){//运营
				model.addAttribute("onProcess", 0);
				model.addAttribute("onProcessBySon", 1);
				menuId = MenuId.THR_OperateCompanyApply_By_Archives_Operations;
			}else if(editType.equals("markets")){//市场
				model.addAttribute("onProcess", 0);
				model.addAttribute("onProcessBySon", 0);
				menuId = MenuId.THR_OperateCompanyApply_By_Archives_Markets;
			}else if(editType.equals("manage")){//管理
				model.addAttribute("onProcess", 0);
				model.addAttribute("onProcessBySon", 0);
				menuId = MenuId.THR_OperateCompanyApply_By_Archives_Manage;
			}else {//详情
				model.addAttribute("onProcess", 1);
				model.addAttribute("onProcessBySon", 1);
				menuId = MenuId.THR_OperateCompanyApply_By_Archives_Detail;
			}

			commonApiService.toEdit(model, id, new ThrOperateCompanyApply(), menuId, extraParam,null);
			return "hr/thrOperateCompanyApply/toEdit";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}


	@Data
	@AllArgsConstructor
	private static class DetailList{
		private Long id;
		private String vgsbm;
		private String vgsjc;
		private List<Map<String, Object>> vpsList;//运营公司表VPS信息
		private List<Map<String, Object>> brandList;//运营公司表品牌信息
		private List<Map<String, Object>> europeTradeList;//运营公司表欧洲贸易合规
	}


}






